package edu.cmich.cps.cps240.partyprincesspalace.hw06;

import java.awt.Rectangle;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;

/**
 * An output wire.  Treated as chip by the workspace for implementation
 * reasons.  Produces no ouput within the workspace itself, but accepts
 * input.  This will send an output value to the "outside" of the circuit.
 * @author holde1ja
 */
public class OutputBus extends Chip implements Serializable{
	private static final long serialVersionUID = 7638444579727938000L;
	//Our first OutputBus is going to be created by the toolbox.  So the toolbox
	//will take OutputBus #-1
	private static int obcount = -1;


	/**
	 * Constructs a new OutputBus, with the given name, and with the location
	 * and shape defined by the given rectangle (just does a shallow copy of
	 * the rectangle)
	 * @param p_name The name that this input bus will have.
	 * @param p_rect The GUI representation of this 
	 */
	public OutputBus(Rectangle p_rect)
	{
		super(p_rect, new Vector<ChipPin>(), new Vector<ChipPin>(), getNewName());
		inputs.add(new ChipPin(name, this, null));
	}
	
	/**
	 * Calls the chip copy constructor
	 * @param other An OutputBus that we wish to make a copy of
	 */
	public OutputBus(OutputBus other)
	{
		super(other);
		this.setName(getNewName());

	}
	
	/**
	 * @return a new name based on the number of OutputBusses that have been created
	 */
	private static String getNewName(){
		String ret = "out" + obcount;
		obcount++;
		return ret;
	}
	
	/**
	 * Changes the name of the OutputBus.  OutputBuses, unlike normal Chips,
	 * can and SHOULD have a unique name
	 * @param p_name The new name for this OutputBus
	 */
	public void setName(String p_name)
	{
		name = p_name;
		if (inputs.size() > 0 && inputs.get(0) != null && inputs.get(0).getPartner() != null)
		{
			((Wire)inputs.get(0).getPartner().getParent()).updateName(inputs.get(0));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see edu.cmich.cps.cps240.partyprincesspalace.hw06.Chip#writeObject(java.io.ObjectOutputStream)
	 */
	public void writeObject(ObjectOutputStream out) throws IOException
	{
		out.defaultWriteObject(); 
	}
	
	/*
	 * (non-Javadoc)
	 * @see edu.cmich.cps.cps240.partyprincesspalace.hw06.Chip#readObject(java.io.ObjectInputStream)
	 */
	public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		in.defaultReadObject();
	
	}
}